package com.android.aircondition.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.aircondition.R;
import com.android.aircondition.constant.AppConstant;
import com.android.aircondition.service.Auxiliary;
import com.android.aircondition.service.GreeService;

public class SubTabAuxiliaryView extends LinearLayout implements OnClickListener{
	
	private LinearLayout firstLinear;
	private LinearLayout secondLinear;
	private LinearLayout thirdLinear;
	private LinearLayout fourthLinear;
	private LinearLayout fifthLinear;
	
	private ImageView   firstImv;
	private ImageView   secondImv;
	private ImageView   thirdImv;
	private ImageView   fourthImv;
	private ImageView   fifthImv;

	private TextView    firstText;
	private TextView    secondText;
	private TextView    thirdText;
	private TextView    fourthText;
	private TextView	fifthText;

	
	private Boolean key;
	
	private GreeService service;
	
	public SubTabAuxiliaryView(Context context) {
		super(context,null);
	}

	public SubTabAuxiliaryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		LinearLayout linear = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.sub_direction, this, true);
		
		firstLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_first);
		secondLinear = (LinearLayout)linear.findViewById(R.id.Direction_subtab_second);
		thirdLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_third);
		fourthLinear = (LinearLayout)linear.findViewById(R.id.Direction_subtab_fourth);
		fifthLinear  = (LinearLayout)linear.findViewById(R.id.Direction_subtab_fifth);
		
		
		firstImv     = (ImageView   )linear.findViewById(R.id.sub_imv_first);
		secondImv    = (ImageView   )linear.findViewById(R.id.sub_imv_second);
		thirdImv     = (ImageView   )linear.findViewById(R.id.sub_imv_third);
		fourthImv    = (ImageView   )linear.findViewById(R.id.sub_imv_fourth);
		fifthImv     = (ImageView   )linear.findViewById(R.id.sub_imv_fifth);

		
		firstText    = (TextView    )linear.findViewById(R.id.sub_tv_first);
		secondText   = (TextView    )linear.findViewById(R.id.sub_tv_second);
		thirdText    = (TextView    )linear.findViewById(R.id.sub_tv_third);
		fourthText   = (TextView    )linear.findViewById(R.id.sub_tv_fourth);
		fifthText    = (TextView    )linear.findViewById(R.id.sub_tv_fifth);

		
		initData(context);
		
	}

	private void initData(Context context){
		
		
		
		firstImv .setImageResource(R.drawable.sub_tab_jinghua);
		secondImv.setImageResource(R.drawable.sub_tab_ganzao);
		thirdImv .setImageResource(R.drawable.sub_tab_dengguang);
		fourthImv.setImageResource(R.drawable.sub_tab_kuaileng);
		fifthImv .setImageResource(R.drawable.sub_tab_kuaileng);
		
		
		String[] array = context.getResources().getStringArray(R.array.sub_tab_auxiliary);
		firstText .setText(array[AppConstant.NO_0]);
		secondText.setText(array[AppConstant.NO_1]);
		thirdText .setText(array[AppConstant.NO_2]);
		fourthText.setText(array[AppConstant.NO_3]);
		fifthText .setText(array[AppConstant.NO_3]);

		
		firstLinear .setOnClickListener(this);
		secondLinear.setOnClickListener(this);
		thirdLinear .setOnClickListener(this);
		fourthLinear.setOnClickListener(this);

		
		service = new GreeService(context);
		Auxiliary auxiliary = service.getAuxiliaryPreference();
		
		int alphaJinghua   = auxiliary.getJinghua()?255:150;
		int alphaGanzao    = auxiliary.getGanzao()?255:150;
		int alphaDengguang = auxiliary.getDengguang()?255:150;
		int alphaKuaileng  = auxiliary.getKuaileng()?255:150;

		
		firstImv.setAlpha(alphaJinghua);
		secondImv.setAlpha(alphaGanzao);
		thirdImv.setAlpha(alphaDengguang);
		fourthImv.setAlpha(alphaKuaileng);
	
		key = false;
		
		fifthLinear.setVisibility(View.INVISIBLE);
	}
	
	public void onClick(View v) {
		
		if(key) return;
		
		key = true;
		
		Auxiliary auxiliary = service.getAuxiliaryPreference();
		
		switch (v.getId()) {
		case R.id.Direction_subtab_first:
			
			if(auxiliary.getJinghua()){
				firstImv.setAlpha(150);
				auxiliary.setJinghua(false);
			}else{
				firstImv.setAlpha(255);
				auxiliary.setJinghua(true);
			}
			
			
			break;
		case R.id.Direction_subtab_second:
			
			if(auxiliary.getGanzao()){
				secondImv.setAlpha(150);
				auxiliary.setGanzao(false);
			}else{
				secondImv.setAlpha(255);
				auxiliary.setGanzao(true);
			}
			break;
		case R.id.Direction_subtab_third:
			
			if(auxiliary.getDengguang()){
				thirdImv.setAlpha(150);
				auxiliary.setDengguang(false);
			}else{
				thirdImv.setAlpha(255);
				auxiliary.setDengguang(true);
			}
			break;
		case R.id.Direction_subtab_fourth:
			
			if(auxiliary.getKuaileng()){
				fourthImv.setAlpha(150);
				auxiliary.setKuaileng(false);
			}else{
				fourthImv.setAlpha(255);
				auxiliary.setKuaileng(true);
			}
			break;


		}
		
		service.setAuxiliaryPreference(auxiliary);
		
		key = false;
	}
	
//	private void resetSubTabStatus(){
//		firstImv .setAlpha(150);
//		secondImv.setAlpha(150);
//		thirdImv .setAlpha(150);
//		fourthImv.setAlpha(150);
//	}

	
}
